package com.lagou.rocketmq.listener;

import com.lagou.rocketmq.dao.GoodDao;
import com.lagou.rocketmq.dao.OrdersDao;
import com.lagou.rocketmq.pojo.Orders;
import com.lagou.rocketmq.service.GoodService;
import com.lagou.rocketmq.service.OrdersService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.transaction.Transactional;

@Component
@RocketMQMessageListener(topic = "topic-1", consumerGroup = "topic-1-consumer-1")
public class PayOrder implements RocketMQListener<String> {

    @Resource
    private GoodService goodService;
    @Resource
    private OrdersDao ordersDao;

    @Transactional
    @Override
    public void onMessage(String s) {
        System.out.println("订单创建消息:" + s);
        Integer ordersId = Integer.valueOf(s);
        Orders orders = ordersDao.findById(ordersId).get();
        if (orders.getState() == 0) {
            orders.setState(2);
            ordersDao.save(orders);
            goodService.inventoryAddById(orders.getGoodId());
            System.out.println("订单未支付:" + ordersId);
        } else {
            System.out.println("订单已支付:" + ordersId);
        }
    }

}
